﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using System.Data;
using BUS;
using DTO;

namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for BaoCaoCongNoUserControl.xaml
    /// </summary>
    public partial class BaoCaoCongNoUserControl : UserControl
    {
        private Section_DTO Section;
        private MainWindow _parent;

        public BaoCaoCongNoUserControl(Section_DTO Sect, MainWindow par)
        {
            Section = Sect;
            _parent = par;
            InitializeComponent();
            reportBCCN.Load +=reportBCCN_Load;
        }

        private void reportBCCN_Load(object sender, EventArgs e)
        {
            TaoBaoCao();
        }

        /// <summary>
        /// 
        /// </summary>
        private void TaoBaoCao()
        {
            string day = System.DateTime.Now.ToString();// "1/" + tbThang.Text + "/" + tbNam.Text;

            DataTable baocao = BaoCaoCongNo_BUS.BAOCAOCONGNO.TaoBaoCaoCongNo(day);

            ThemBaoCao(day, baocao);

            LoadBaoCao();
            
        }
        
        private void LoadBaoCao()
        {
            
            ReportDataSource reportDataSource = new ReportDataSource();
            QLNSDataSet dataset = new QLNSDataSet();
            System.Windows.Forms.BindingSource binding = new System.Windows.Forms.BindingSource();
          
            dataset.BeginInit();
            reportDataSource.Name = "DataSetBCCN";
            binding.DataMember = "TaoBaoCaoCongNo";
            binding.DataSource = dataset;
            reportDataSource.Value = binding;
            this.reportBCCN.LocalReport.DataSources.Add(reportDataSource);
            this.reportBCCN.LocalReport.ReportPath = "../../BCCNReport.rdlc";                    
            dataset.EndInit();

            QLNSDataSetTableAdapters.TaoBaoCaoCongNoTableAdapter
            accountsTableAdapter = new QLNSDataSetTableAdapters.TaoBaoCaoCongNoTableAdapter();
 
            accountsTableAdapter.ClearBeforeFill = true;
            accountsTableAdapter.Fill(dataset.TaoBaoCaoCongNo, System.DateTime.Now);
            this.reportBCCN.RefreshReport();
        }

        /// <summary>
        /// Them Bao Cao va Chi Tiet Bao Cao vao CSDL
        /// </summary>
        /// <param name="day"></param>
        /// <param name="chitiet"></param>
        private void ThemBaoCao(string day, DataTable chitiet)
        {
            // Them Bao Cao Cong No vao co so du lieu
            BaoCaoCongNo_DTO bc = new BaoCaoCongNo_DTO(day);
            BaoCaoCongNo_BUS.BAOCAOCONGNO.ThemBaoCaoCongNo(bc);

            // Them Chi Tiet Bao Cao vao co so du lieu
            int ma = BaoCaoCongNo_BUS.BAOCAOCONGNO.LayMaBaoCaoCongNoLonNhat();
            
            int r = chitiet.Rows.Count;
            
            for (int i = 0; i < r; i++)
            {
                int makh = Int32.Parse(chitiet.Rows[i]["MaKhachHang"].ToString());
                float nodau = float.Parse(chitiet.Rows[i]["NoDau"].ToString());
                float phatsinh = float.Parse(chitiet.Rows[i]["PhatSinh"].ToString());
                float nocuoi = float.Parse(chitiet.Rows[i]["NoCuoi"].ToString());
                
                ChiTietBaoCaoCongNo_DTO ct = new ChiTietBaoCaoCongNo_DTO(ma, makh, nodau, phatsinh, nocuoi);
                ChiTietBaoCaoCongNo_BUS.CHITIETBAOCAOCONGNO.ThemChiTietBaoCaoCongNo(ct);
            }
        }
    }
}
